package com.bawei.module_play.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.RoutePath
import com.bawei.lib_common.mvi.BaseMviFragment
import com.bawei.module_play.R
import com.bawei.module_play.adapter.PlayListAdapter
import com.bawei.module_play.databinding.FragmentCommentBinding
import com.bawei.module_play.entriy.AuthorEntriy
import com.bawei.module_play.entriy.CommendEntity
import com.bawei.module_play.entriy.CommentEntriyItem
import com.bawei.module_play.entriy.CommentParams
import com.bawei.module_play.entriy.Reply
import com.bawei.module_play.intent.PlayVideoIntent
import com.bawei.module_play.state.PlayVideoState
import com.bawei.module_play.vm.PlayViewModel
import com.example.image.glide.GlideImgLoader
import com.luck.picture.lib.utils.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RoutePath.PATH_FRAGMENT_DETAIL_COMMEND)
class CommentFragment : BaseMviFragment<FragmentCommentBinding>() {
    @Autowired
    @JvmField var itemId: String = ""

    @Autowired
    @JvmField var moduleType: Int = 0

    private val mViewModel: PlayViewModel by activityViewModels()
    private var commVideoList= mutableListOf<CommentEntriyItem>()
    private lateinit var commlistadapter:PlayListAdapter


    override fun getLayoutId(): Int {
        return R.layout.fragment_comment
    }

    override fun initViews() {
        ARouter.getInstance().inject(this)
        lifecycleScope.launch {
            mViewModel.channel.send(PlayVideoIntent.LoadCommentByUserId(0,itemId))
        }

        //点击发送评论
        mDatatBinding.send.setOnClickListener {
            lifecycleScope.launch {
                mViewModel.channel.send(PlayVideoIntent.PublishVideoComment(
                    CommendEntity(
                    0,
                    "",
                    "",
                    0,
                    0,
                    0,
                    "",
                    listOf(),
                    0,
                    0,
                    "",
                )
                ))}
        }
        commlistadapter=PlayListAdapter(commVideoList)
        mDatatBinding.commiv.adapter = commlistadapter

    }

    override suspend fun collectUIState() {
        mViewModel.uiStateFlow.collect{
            when(it){
                is PlayVideoState.LoadVideoCommentSuccess -> updataPageDate(it.list)
                is PlayVideoState.LoadVideoCommentFail -> {
                    ToastUtils.showToast(requireActivity(),"加载失败")
                }
                is PlayVideoState.PublishVideoCommentSuccess -> {
                    ToastUtils.showToast(requireActivity(),"评论成功")
                    mDatatBinding.comment.setText("")
                    lifecycleScope.launch {
                        mViewModel.channel.send(PlayVideoIntent.LoadCommentByUserId(0,itemId))
                    }
                }
                is PlayVideoState.PublishVideoCommentFail -> {
                    ToastUtils.showToast(requireActivity(),"评论失败")
                }
                else->{

                }
            }
        }
    }




    private fun updataPageDate(list: List<CommentEntriyItem>){
      commVideoList.addAll( list)
        commlistadapter.notifyDataSetChanged()
    }

}